package com.runchen.broadcastdetection.utils;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.ruochen.common.base.BaseActivity;

/**
 * Author: SongJun.
 * CreateDate: 2019/3/1 0001.
 * Description:
 * Glide特点
 * 使用简单
 * 可配置度高，自适应程度高
 * 支持常见图片格式 Jpg png gif webp
 * 支持多种数据源  网络、本地、资源、Assets 等
 * 高效缓存策略    支持Memory和Disk图片缓存 默认Bitmap格式采用RGB_565内存使用至少减少一半
 * 生命周期集成   根据Activity/Fragment生命周期自动管理请求
 * 高效处理Bitmap  使用Bitmap Pool使Bitmap复用，主动调用recycle回收需要回收的Bitmap，减小系统回收压力
 * 这里默认支持Context，Glide支持Context,Activity,Fragment，FragmentActivity
 */
public class GlideUtil {


    public static boolean isValidContextForGlide(final Context context) {
        if (context == null) {
            return true;
        }
        if (context instanceof BaseActivity) {
            final BaseActivity activity = (BaseActivity) context;
            return activity.isFinish();
        }
        return false;
    }

    /**
     * 默认加载
     *
     * @param mContext
     * @param path
     * @param mImageView
     */
    public static void loadImageView(Context mContext, String path, ImageView mImageView) {
        if (isValidContextForGlide(mContext)) return;
        if (TextUtils.isEmpty(path)) {
            return;
        }
        Glide.with(mContext).load(path).into(mImageView);
    }

    /**
     * 默认加载
     *
     * @param mContext
     * @param drawable
     * @param mImageView
     */
    public static void loadImageView(Context mContext, int drawable, ImageView mImageView) {
        if (isValidContextForGlide(mContext)) return;
        if (drawable <= 0) {
            return;
        }
        Glide.with(mContext).load(drawable).into(mImageView);
    }

    /**
     * 设置加载中以及加载失败图片
     *
     * @param mContext
     * @param path
     * @param mImageView
     * @param loadingImage
     * @param errorImage
     */
    public static void loadImageViewLoading(Context mContext, String path, ImageView mImageView, int loadingImage, int errorImage) {
        if (isValidContextForGlide(mContext)) return;
        if (TextUtils.isEmpty(path)) {
            mImageView.setImageResource(errorImage);
            return;
        }
        Glide.with(mContext).load(path)
                .apply(new RequestOptions()
                        .placeholder(loadingImage)
                        .error(errorImage)
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .into(mImageView);
    }

    /**
     * 设置缩略图支持
     * 会先加载缩略图
     */
    public static void loadImageViewThumbnail(Context mContext, String path, ImageView mImageView) {
        if (isValidContextForGlide(mContext)) return;
        if (TextUtils.isEmpty(path)) {
            return;
        }
        Glide.with(mContext).load(path).thumbnail(0.1f).into(mImageView);
    }

    /**
     * * api提供了比如：centerCrop()、fitCenter()等
     * 设置动态转换
     *
     * @param mContext
     * @param path
     * @param mImageView
     */
    public static void loadImageViewCrop(Context mContext, String path, ImageView mImageView) {
        if (isValidContextForGlide(mContext)) return;
        if (TextUtils.isEmpty(path)) {
            return;
        }
        Glide.with(mContext).load(path)
                .apply(new RequestOptions()
                        .centerCrop()
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .into(mImageView);
    }

    public static void loadImageViewCrop(Context mContext, String path, ImageView mImageView, int placeholder, int error) {
        if (isValidContextForGlide(mContext)) return;
        if (TextUtils.isEmpty(path)) {
            mImageView.setImageResource(placeholder);
            return;
        }
        Glide.with(mContext).load(path)
                .apply(new RequestOptions()
                        .centerCrop()
                        .placeholder(placeholder)
                        .error(error)
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .into(mImageView);
    }

    public static void loadImageViewFitXY(Context mContext, String path, ImageView mImageView, int placeholder, int error) {
        if (isValidContextForGlide(mContext)) return;
        if (TextUtils.isEmpty(path)) {
            mImageView.setImageResource(placeholder);
            return;
        }
        Glide.with(mContext).load(path)
                .apply(new RequestOptions()
                        .fitCenter()
                        .placeholder(placeholder)
                        .error(error)
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .into(mImageView);
    }

    public static void loadCircleImage(Context mContext, String path, ImageView mImageView) {
        if (isValidContextForGlide(mContext)) return;
        if (TextUtils.isEmpty(path)) {
            return;
        }
        Glide.with(mContext).load(path)
                .apply(new RequestOptions()
                        .circleCrop()
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .into(mImageView);
    }

    public static void loadImageViewCircleCrop(Context mContext, String path, ImageView mImageView) {
        if (isValidContextForGlide(mContext)) return;
        if (TextUtils.isEmpty(path)) {
            return;
        }
        Glide.with(mContext).load(path)
                .apply(new RequestOptions()
                        .circleCrop()
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .into(mImageView);
    }


    public static void loadImageViewCircleCrop(Context mContext, String path, ImageView mImageView, int placeholder, int error) {
        if (isValidContextForGlide(mContext)) return;
        if (TextUtils.isEmpty(path)) {
            mImageView.setImageResource(placeholder);
            return;
        }
        Glide.with(mContext).load(path)
                .apply(new RequestOptions()
                        .circleCrop()
                        .placeholder(placeholder)
                        .error(error)
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .into(mImageView);
    }

    public static void loadImageViewRoundCrop(Context mContext, String path, int roundDp, ImageView mImageView, int placeholder, int error) {
        if (isValidContextForGlide(mContext)) return;
        if (TextUtils.isEmpty(path)) {
            mImageView.setImageResource(placeholder);
            return;
        }
        //圆角
        RequestOptions myOptions = new RequestOptions()
                .placeholder(placeholder).error(error)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .transform(new GlideRoundTransform(mContext, roundDp));
        Glide.with(mContext).load(path)
                .apply(myOptions)
                .placeholder(placeholder)
                .error(error)
                .into(mImageView);
    }

    public static void loadImageViewRoundFit(Context mContext, String path, int roundDp, ImageView mImageView, int placeholder, int error) {
        if (isValidContextForGlide(mContext)) return;
        if (TextUtils.isEmpty(path)) {
            mImageView.setImageResource(placeholder);
            return;
        }
        //圆角
        RequestOptions myOptions = new RequestOptions()
                .placeholder(placeholder).error(error)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .fitCenter()
                .transform(new GlideRoundTransform(mContext, roundDp));
        Glide.with(mContext).load(path)
                .apply(myOptions)
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(mImageView);
    }

    public static void loadImageViewCustomerRoundCrop(Context mContext, String path, ImageView mImageView,
                                                      int placeholder, int error,
                                                      boolean leftTop, boolean rightTop, boolean leftBottom, boolean rightBottom,
                                                      int rundDp) {
        RoundedCornersTransform transform = new RoundedCornersTransform(mContext, rundDp);
        transform.setNeedCorner(leftTop, rightTop, leftBottom, rightBottom);
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(placeholder)
                .error(error)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(transform);
        Glide.with(mContext).load(path)
                .apply(options)
                .into(mImageView);
    }

    /**
     * 清理磁盘缓存
     *
     * @param mContext
     */
    public static void GuideClearDiskCache(Context mContext) {
        //理磁盘缓存 需要在子线程中执行
        Glide.get(mContext).clearDiskCache();
    }

    /**
     * 清理内存缓存
     *
     * @param mContext
     */
    public static void GuideClearMemory(Context mContext) {
        //清理内存缓存  可以在UI主线程中进行
        Glide.get(mContext).clearMemory();
    }

}
